package com.jams.goodox.activity.mine.bill;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.entity.BillDetail;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.MineService;
import com.jams.goodox.utils.CollectionUtils;
import com.jams.goodox.utils.ProgressDialogUtils;
import com.jams.goodox.utils.StringUtils;
import com.jams.goodox.utils.StyleUtil;
import com.jams.goodox.widget.LRTextView;

import java.util.List;

import butterknife.Bind;
import retrofit2.Call;

/**
 * Created by jams on 2017/7/12
 * ***
 * Tel: ***
 * Desc: 账单详情页
 */
public class BillDetailActivity extends BaseActivity {

    @Bind(R.id.tv_change_money)
    TextView tvChangeMoney;  //交易金额
    @Bind(R.id.lr_bill_type)
    LRTextView lrBillType;  //类型
    @Bind(R.id.lr_bill_time)
    LRTextView lrBillTime;  //时间
    @Bind(R.id.lr_bill_pay_type)
    LRTextView lrBillPayType;  //支付方式
    @Bind(R.id.lr_bill_balance)
    LRTextView lrBillBalance;  //交易后余额
    @Bind(R.id.lr_bill_refund_action)
    LRTextView lrBillRefundAction;  //退款操作
    @Bind(R.id.lr_bill_refund_action_person)
    LRTextView lrBillRefundActionPerson;  //操作人
    @Bind(R.id.lr_bill_order_id)
    LRTextView lrBillOrderId;  //订单号
    @Bind(R.id.lr_bill_server_name)
    LRTextView lrBillServerName;  //服务名称
    @Bind(R.id.lr_bill_product_detail)
    LRTextView lrBillProductDetail;  //商品明细
    @Bind(R.id.lr_bill_content_type)
    LRTextView lrBillContentType;  //内容里面的类型
    @Bind(R.id.lr_bill_pay_detail)
    LRTextView lrBillPayDetail;  //缴费详情
    @Bind(R.id.lv_content_group)
    LinearLayout lvContentGroup;  //内容布局

    public final static String BILL_ID = "billId";
    public final static int RECHARGE = 1;//充值
    public final static int CONSUMPTION = 2;//消费
    public final static int REFUND = 3;//退款
    @Bind(R.id.lr_bill_address)
    LRTextView lrBillAddress;
    @Bind(R.id.lv_bill_all)
    LinearLayout lvBillAll;
    private BillDetail.DataBean bean;
    private String id;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_bill_detail;
    }

    @Override
    protected void init() {
        StyleUtil.init(this, "账单详情");
        id = getIntent().getStringExtra(BILL_ID);
        getBillDetail();
    }

    //获取账单详情
    private void getBillDetail() {
        ProgressDialogUtils.show(this);
        enqueue(createCallService(MineService.class).getBillDetail(id), new RetrofitCallbackListener<BillDetail>() {
            @Override
            public void onSuccess(Call<BillDetail> call, BillDetail result) {
                ProgressDialogUtils.dismiss();
                bean = result.getData();
                setData();
            }

            @Override
            public void onError(String msg) {
                ProgressDialogUtils.dismiss();
            }
        });
    }

    //设置账单数据
    private void setData() {
        if (bean == null) {
            lvBillAll.setVisibility(View.INVISIBLE);
            return;
        }
        //设置交易金额
        tvChangeMoney.setText(bean.getMoneyChangeStr());
        //设置时间
        lrBillTime.setTvRight(bean.getIntime());
        //根据账单类型设置不同的布局
        switch (bean.getType()) {
            case RECHARGE://充值
                setRechargeView();
                break;
            case CONSUMPTION://消费
                setConsumption();
                break;
            case REFUND://退款
                setRefundView();
                break;
            default:
                break;
        }
        lvBillAll.setVisibility(View.VISIBLE);
    }

    //设置充值布局
    private void setRechargeView() {
        lrBillType.setTvRight("充值");
        lrBillPayType.setTvLeft("来源");
        //设置来源
        lrBillPayType.setTvRight(bean.getPayType(mContext));
        //设置交易后余额
        lrBillBalance.setTvRight(StringUtils.withMoney(bean.getMoney()));

        //目前 0 是普通充值，34为猎头佣金获得 ，但是还有11，11是混合支付会出现的，现在没有混合支付
        if(bean.getSpecialType() == 34){
            setHeadhunter();
        }else{
            //隐藏“内容”模块
            lvContentGroup.setVisibility(View.GONE);
        }
    }

    //设置消费布局
    private void setConsumption() {
        lrBillType.setTvRight("消费");
        lrBillPayType.setTvLeft("支付方式");
        BillDetail.DataBean.ListBean listBean = null;
        if (CollectionUtils.hasData(bean.getList())) {
            listBean = bean.getList().get(0);
        }
        //设置支付方式
        lrBillPayType.setTvRight(bean.getPayType(mContext));
        //如果是使用余额消费的则显示“交易后余额”
        if (bean.getMoneyType() == 5) {
            //设置交易后余额
            lrBillBalance.setTvRight(StringUtils.withMoney(bean.getMoney()));
        } else {
            lrBillBalance.setVisibility(View.GONE);
        }
        /*
         * 根据订单类型设置操作人
         * identity == 1,表示是企业账单
         * 企业账单显示操作人
         */
        if (listBean != null && listBean.getIdentity() == 1) {
            lrBillRefundActionPerson.setVisibility(View.VISIBLE);
            lrBillRefundActionPerson.setTvRight(listBean.getPayUserName() + "\n" + listBean.getPayUserPhone());
        } else {
            lrBillRefundActionPerson.setVisibility(View.GONE);
        }
        //根据消费类型来设置不同的布局
        switch (bean.getSpecialType()) {
            case 21://订单消费
                lrBillOrderId.setTvLeft("订单号");
                setOrderBill();
                break;
            case 23://猎头佣金
                lrBillOrderId.setTvLeft("付款单号");
                setHeadhunter();
                break;
            case 43://物业缴费
                lrBillOrderId.setTvLeft("付款单号");
                setProperty(listBean);
                break;
            case 45://停车申请
                lrBillOrderId.setTvLeft("付款单号");
                setCarView();
                break;
            case 46://场地预定
                lrBillOrderId.setTvLeft("付款单号");
                setPlaceView();
                break;
            case 24://扫码付款
                lrBillOrderId.setTvLeft("付款单号");
                setScanPayView();
                break;
            default:
                break;
        }
    }

    //设置订单消费布局
    private void setOrderBill(){
        //设置订单号
        lrBillOrderId.setTvRight(bean.getSerialNumber());
        //设置服务名称
        lrBillServerName.setTvRight(bean.getSubName());
        List<BillDetail.DataBean.ItemListBean> list = bean.getItemList();
        if(!CollectionUtils.hasData(list)) return;
        String s = "";
        for (int i = 0; i < list.size(); i++) {
            if (i == 0) {
                s += list.get(i).getItemName() + "\n￥" + list.get(i).getItemPrice() + list.get(i).getItemUnit() + " x" + list.get(i).getItemNum();
            } else {
                s += "\n\n" + list.get(i).getItemName() + "\n￥" + list.get(i).getItemPrice() + list.get(i).getItemUnit() + " x" + list.get(i).getItemNum();
            }
        }
        //设置商品明细
        lrBillProductDetail.setTvRight(s);
    }

    //设置停车申请
    private void setCarView(){
        //设置订单号
        lrBillOrderId.setTvRight(bean.getSerialNumber());
        //设置服务名称
        lrBillServerName.setTvRight(bean.getSubName());
        List<BillDetail.DataBean.PlaceItem> list = bean.getPlaceList();
        if(!CollectionUtils.hasData(list)) return;
        String s = "";
        for (int i = 0; i < list.size(); i++) {
            if (i == 0) {
                s += list.get(i).getCarNum() + "\n" + list.get(i).getMenuCost() + getMenuType(list.get(i).getMenuType()) + " x"+list.get(i).getBuyNum()+"\n" + list.get(i).getStartTime()+"~"+list.get(i).getEndTime();
            } else {
                s += "\n\n" + list.get(i).getCarNum() + "\n" + list.get(i).getMenuCost() + getMenuType(list.get(i).getMenuType()) + " x"+list.get(i).getBuyNum()+"\n" + list.get(i).getStartTime()+"~"+list.get(i).getEndTime();
            }
        }
        //设置商品明细
        lrBillProductDetail.setTvRight(s);
    }

    //设置场地预定布局
    private void setPlaceView(){
        //设置订单号
        lrBillOrderId.setTvRight(bean.getSerialNumber());
        //设置服务名称
        lrBillServerName.setTvRight(bean.getSubName());
        List<BillDetail.DataBean.ListBean> list = bean.getList();
        if(!CollectionUtils.hasData(list)) return;
        String detail = "";
        for (int i = 0; i < list.size(); i++) {
            if (i == 0) {
                detail += list.get(i).getAddress() + "\n" + list.get(i).getDate()+"  "+list.get(i).getBegin()+"-"+list.get(i).getEnd()+"\n"+list.get(i).getPrice()+
                        "元/小时  x"+list.get(i).getTimeCount();
            } else {
                detail += "\n" + list.get(i).getDate()+"  "+list.get(i).getBegin()+"-"+list.get(i).getEnd()+"\n"+list.get(i).getPrice()+
                        "元/小时  x"+list.get(i).getTimeCount();
            }
        }
        //设置商品明细
        lrBillProductDetail.setTvRight(detail);
    }

    //设置扫码付款
    private void setScanPayView(){
        //设置订单号
        lrBillOrderId.setTvRight(bean.getSerialNumber());
        //设置服务名称
        lrBillServerName.setTvRight(bean.getShopName());
        lrBillProductDetail.setVisibility(View.GONE);
    }
    //设置物业缴费布局
    private void setProperty(BillDetail.DataBean.ListBean listBean){
        //设置付款单号
        lrBillOrderId.setTvRight(bean.getSerialNumber());
        //隐藏服务名称
        lrBillServerName.setVisibility(View.GONE);
        //隐藏商品明细
        lrBillProductDetail.setVisibility(View.GONE);
        //显示内容类型
        lrBillContentType.setVisibility(View.VISIBLE);
        //显示收货地址
        lrBillAddress.setVisibility(View.VISIBLE);
        //显示缴费详情
        lrBillPayDetail.setVisibility(View.VISIBLE);
        //设置内容类型
        if (listBean != null) {
            lrBillContentType.setTvRight(listBean.getItem());
            lrBillAddress.setTvRight(listBean.getAddress());
            List<BillDetail.DataBean.ListBean.MonthListBean> monthListBeen =listBean.getMonthList();
            String month = "";
            for (int i = 0; i < monthListBeen.size(); i++) {
                if (i == 0) {
                    month += monthListBeen.get(i).getBillYear() + "年" + monthListBeen.get(i).getBillMonth() + "月 ￥" + monthListBeen.get(i).getTotalPrice();
                } else {
                    month += "\n" + monthListBeen.get(i).getBillYear() + "年" + monthListBeen.get(i).getBillMonth() + "月 ￥" + monthListBeen.get(i).getTotalPrice();
                }
            }
            lrBillPayDetail.setTvRight(month);
        }
    }

    //设置猎头佣金布局
    private void setHeadhunter(){
        //设置付款单号
        lrBillOrderId.setTvRight(bean.getSerialNumber());
        //隐藏服务名称
        lrBillServerName.setVisibility(View.GONE);
        //隐藏商品明细
        lrBillProductDetail.setVisibility(View.GONE);
        //显示内容类型
        lrBillContentType.setVisibility(View.VISIBLE);
        //显示企业名称
        lrBillAddress.setVisibility(View.VISIBLE);
        lrBillAddress.setTvLeft("企业名称：");
        lrBillAddress.setTvRight(bean.getEnterpriseName());
        //显示收款人
        lrBillPayDetail.setVisibility(View.VISIBLE);
        lrBillPayDetail.setTvLeft("收款人");
        lrBillPayDetail.setTvRight(bean.getUserName());
        //设置类型
        lrBillContentType.setTvRight(bean.getRefundReason());
    }

    //设置退款布局
    private void setRefundView() {
        lrBillType.setTvRight("退款");
        lrBillPayType.setTvLeft("来源");
        //设置来源
        lrBillPayType.setTvRight(bean.getPayType(mContext));
        //设置交易后余额
        lrBillBalance.setTvRight(StringUtils.withMoney(bean.getMoney()));
        lrBillRefundAction.setVisibility(View.VISIBLE);
        //设置退款操作
        lrBillRefundAction.setTvRight(String.valueOf(bean.getRefundReason()));
        //设置订单号
        lrBillOrderId.setTvRight(bean.getSerialNumber());
        //设置服务名称
        lrBillServerName.setTvRight(bean.getSubName());
        List<BillDetail.DataBean.ItemListBean> list = bean.getItemList();
        if(list == null){
            return;
        }
        String s = "";
        for (int i = 0; i < bean.getItemList().size(); i++) {
            if (i == 0) {
                s += bean.getItemList().get(i).getItemName() + "\n￥" + bean.getItemList().get(i).getItemPrice() + bean.getItemList().get(i).getItemUnit() + " x" + bean.getItemList().get(i).getItemNum();
            } else {
                s += "\n" + bean.getItemList().get(i).getItemName() + "\n￥" + bean.getItemList().get(i).getItemPrice() + bean.getItemList().get(i).getItemUnit() + " x" + bean.getItemList().get(i).getItemNum();
            }
        }
        //设置商品明细
        lrBillProductDetail.setTvRight(s);
    }

    //获取套餐类型
    private String getMenuType(int type){
        switch (type){
            case 1:
                return "免费";
            case 2:
                return "元/月";
            case 3:
                return "元/年";
            default:
                return "";
        }
    }
}
